package com.boot.system.entity.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.boot.system.entity.SysDept;
import com.boot.system.entity.SysUser;
import com.boot.system.service.DeptService;
import com.mybatisflex.annotation.SetListener;

public class UserOnSetListener implements SetListener {

    @Override
    public Object onSet(Object entity, String property, Object value) {
        SysUser sysUser = (SysUser) entity;
        if (property.equals("roles") && value != null){
            // 将角色字符串转为数组
            sysUser.setRoleArr(value.toString().split(","));
        }
        if (property.equals("deptId") && value != null){
            // 将部门ID转为部门名称
            DeptService deptService = SpringUtil.getBean(DeptService.class);
            SysDept sysDept = deptService.getCache(Long.valueOf(value.toString()), null);
            if (sysDept != null){
                sysUser.setDeptName(sysDept.getName());
            }
        }
        return value;
    }
}
